# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=libkml
pkgver=1.3.0
pkgrel=10
pkgdesc="Reference implementation of OGC KML 2.2"
url="https://github.com/libkml/libkml"
arch="all"
license="BSD-3-Clause"
depends_dev="
	boost-dev
	expat-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	cmake
	curl-dev
	minizip-dev
	py3-setuptools
	python3-dev
	samurai
	swig
	uriparser-dev
	"
checkdepends="
	gtest-dev
	"
subpackages="
	$pkgname-dev
	py3-$pkgname:py
	"
source="https://github.com/libkml/libkml/archive/$pkgver/libkml-$pkgver.tar.gz
	10-fix-mistaken-use-of-std-cerr-instead-of-std-endl.patch
	20-fix-python3.patch
	30-fix-python-tests.patch
	40-dont-bytecompile.patch
	"

# missing openjdk on mips64 and riscv64
case $CARCH in
	mips*|riscv64)
		_have_java=OFF
		;;
	*)
		_have_java=ON
		makedepends="$makedepends java-jdk"
		subpackages="$subpackages java-$pkgname:java"
		;;
esac

build() {
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DWITH_SWIG=ON \
		-DWITH_PYTHON=ON \
		-DWITH_JAVA=$_have_java \
		-DJAVA_HOME=/usr/lib/jvm/default-jvm \
		-DBUILD_TESTING="$(want_check && echo ON || echo OFF)" \
		-DBUILD_EXAMPLES=OFF
	cmake --build build
}

py() {
	pkgdesc="Python 3 bindings for $pkgname"

	amove usr/lib/python3.*
}

java() {
	pkgdesc="Java bindings for $pkgname"

	amove usr/lib/jni
	amove usr/share
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

check() {
	cd build

	# test_java_kmlbase fails on all platforms, engine_feature_view fails on x86
	local excludes="(test_java_kmlbase"
	case "$CARCH" in
		x86) excludes="$excludes|engine_feature_view" ;;
	esac
	excludes="$excludes)"

	CTEST_OUTPUT_ON_FAILURE=TRUE ctest -E $excludes
}
sha512sums="
aa48158103d3af764bf98c1fb4cf3e1356b9cc6c8e79d80b96850916f0a8ccb1dac3a46427735dd0bf20647daa047d10e722ac3da2a214d4c1559bf6d5d7c853  libkml-1.3.0.tar.gz
8fe7bd1aeee0663ade115cbb7f63b528d4a919db6e04095c0222b1a9e32a9b2b04deacd15f3224d2146b0943086af205c179bf5638c9f5562b0977aad3ba65fe  10-fix-mistaken-use-of-std-cerr-instead-of-std-endl.patch
a588a5f86d31f3eeb47d6d2c2281a7f518730e4247847e9dae093ca7f3006e27a4b4ca4e2e003ce31ef72f83096f5cfee3b3f2fb536934f64987cecb791ce12b  20-fix-python3.patch
c29d1e9e00fe5000a9b0e7e13bf4c196c61c47459724d5c20a22714504c1497175bca6723759d02be24ac314f6381f4ff2f1245bd2e2c2b2b344f959b50a7978  30-fix-python-tests.patch
a81cd6b09041060aff7d999280e0d7ea4bb6c819b510afd210ad097e480f444f0f2c779fe1cb2f5ecf0976f27daf66df62a88825e893bb6126909acfd0d74f65  40-dont-bytecompile.patch
"
